package cn.pconline.autoclub.service;

import cn.pconline.autoclub.entity.DynaInfo;
import cn.pconline.autoclub.entity.DynaReply;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.util.DateUtils;
import cn.pconline.passport2.util.T;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.httpclient.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;

/**
 * “动态回复”的审核服务类
 */
public class CensorDynaReplyService extends CensorService {

    // 留言typeId,在bean初始化的时候赋值
    private long typeIdReplyId;
    @Autowired
    private DynaService dynaService;

    public void setTypeIdReplyId(long typeIdReplyId) {
        this.typeIdReplyId = typeIdReplyId;
    }

    /**
     * 审核应用回馈
     *
     * @param interfaceUrl
     * @param url
     * @param status
     * @param version
     * @return
     */
    public String feedback(String interfaceUrl, String url, int status, long version) {

        String result = null;
        // url must format like : http://club.pcauto.com.cn/gzqrcyh/comment/?typeId=100&clubId=200&commentId=300

        Map<String, Long> map = getMapByUrl(url);
        long id = map.get("dynaReplyId");
        long dynaInfoId = map.get("dynaInfoId");
        long typeId = map.get("typeId");
        if (typeId == typeIdReplyId) {
            DynaReply dynaReply = null;
            DynaInfo info = null;
            Long create = map.get("createDate");
            try {
                if (create != null) {
                    Date createDate = DateUtils.createDate(create.intValue());
                    info = geliDao.find(DynaInfo.class, createDate.getTime(), dynaInfoId);
                }
                dynaReply = geliDao.find(DynaReply.class, dynaInfoId, id);
            } catch (DataAccessException e) {

                log.info("审核平台回调留言已不存在:" + id);
            }
            if (dynaReply != null && (version == dynaReply.getUpdateAt().getTime())) {
                //int commentStatus = dynaReply.getCensored();
                switch (status) {
                    //审核通过?
                    case CENSORSYSTEM_RESPONSE_PASS:
                        dynaService.censorDynaReply(dynaReply, DynaReply.CENSORED_PASS, info);
                        break;
                    //审核不通过    
                    case CENSORSYSTEM_RESPONSE_DENY:
                        dynaService.censorDynaReply(dynaReply, DynaReply.CENSORED_NOTPASS, info);
                        log.info("车友会留言审核未通过：" + id);
                        break;
                    default:
                        result = "状态不正确";
                        break;
                }
            }
        }

        return result;
    }

    /**
     * 首次通知审核平台新建“动态回复”的状态
     *
     * @param request
     * @param comment
     */
    public void notifyCensorSystemCreate(HttpServletRequest request, final DynaReply dynaReply, DynaInfo dynaInfo) {
        if (csr_switch) {
            final Map<String, Object> params = convert4Create(request, dynaReply, dynaInfo, true);
            executorService.submit(new CensorCall(receiveHome, params));
        }
    }

    /**
     * 将dynaReply类转换成向审核系统传递的参数,只用在新建动态时
     *
     * @param userProfile
     * @param isNew
     * @return
     */
    private Map<String, Object> convert4Create(HttpServletRequest request, DynaReply dynaReply, DynaInfo dynaInfo,
            boolean isNew) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        User user = null;
        try {
            user = this.geliDao.find(User.class, dynaReply.getCreateBy());
        } catch (EmptyResultDataAccessException e) {
        }
        //标题
        map.put(TITLE, dynaReply.getContent());
        map.put(SITE, site);
        map.put(APP, appName);
        map.put(TYPEID, typeIdReplyId);
        map.put(ISNEW, isNew ? 1 : 0);
        // uri formate : "http://club.pcauto.com.cn/gzqrcyh/comment/?typeId=100&clubId=200&commentId=300";
        String root = "http://" + T.stringValue(request.getHeader("host"), "") + T.stringValue(request.getContextPath(), "");
        map.put(URI, root + "/dyna/getDynaInfoDetail.do?createDate=" + DateUtil.formatDate(dynaInfo.getCreateDate(), "yyyy") + "&dynaReplyId=" + dynaReply.getDynaReplyId() + "&typeId="
                + typeIdReplyId + "&dynaInfoId=" + dynaReply.getDynaInfoId());
        map.put(VERSION, (dynaReply.getUpdateAt().getTime() / 1000) * 1000);
        if (user != null) {
            map.put(USERID, user.getUid());
            map.put(USERNAME, user.getUsername());
        }
        map.put(IP, T.getIp(request));
        map.put(CONTENT, dynaReply.getContent());

        return map;
    }

    @Override
    public boolean isCorrectFeedback(String url) {
        Map<String, Long> map = getMapByUrl(url);
        long typeId = 0;
        if (map != null && !map.isEmpty()) {
            typeId = map.get("typeId");
        }
        return typeIdReplyId == typeId;
    }
}
